`<template>
  <div class="wrap">
    <div class="title">
      <img src="../../images/main/par.png" alt>
      <span class="back" @click="backParMan()">服务商管理</span>
      <img class="right" src="../../images/main/right.png" alt>
      <span>{{$route.query.id ? '修改服务商' : '新增服务商'}}</span>
    </div>
    <div class="clear"></div>
    <div class="body">
      <div class="list firlist">
        <img class="red" src="../../images/main/red.png" alt>
        <span class="txt">服务商/企业名称：</span>
        <input type="text" class="input" placeholder="请输入服务商/企业名称" v-model="userInfo.partnerName">
      </div>
      <div class="list">
        <img class="red" src="../../images/main/red.png" alt>
        <span class="txt">服务商账号：</span>
        <input v-if="!$route.query.id" type="text" class="input" placeholder="请输入服务商账号" v-model="userInfo.partnerAccount">
        <span style="padding:0 10px;" v-else type="text" class="input">{{userInfo.partnerAccount}}</span>
        <span class="tips">服务商账号初始密码为：yunlehui2019</span>
      </div>
      <div class="list">
        <img class="red" src="../../images/main/red.png" alt>
        <span class="txt">联系人：</span>
        <input type="text" class="input" placeholder="请输入联系人名称" v-model="userInfo.contacts">
      </div>
      <div class="list">
        <img class="red" src="../../images/main/red.png" alt>
        <span class="txt">手机号：</span>
        <input type="text" class="input" placeholder="请输入手机号" v-model="userInfo.partnerPhone">
      </div>
      <div class="upload">
        <div class="uptitle">上传资质证明（营业执照）：</div>

         <div
          class="upimg"
          :style="{
            boxSizing: 'border-box',
            paddingLeft: '200px'
          }"
          >
          <img :src="userInfo.businessLicense" style="margin: 0"/>
          <el-upload
            class="el-upload"
            :action="`${$store.state.baseUrl}/backstage/api/tPartner/upload`"
            list-type="picture-card"
            :on-success="elUploadSuccess"
            :before-upload="elUploadBefore">
            <i class="el-icon-plus"></i>
          </el-upload>
        </div>

        <!-- <div class="upimg">
          <img v-if="isEdit" :src="userInfo.businessLicense" alt>
          <img v-else src="../../images/main/upimg.png" alt>
          <div class="info">
            <div class="uptxt firtxt">温馨提示：</div>
            <div class="uptxt">建议上传尺寸为960*675，大小不超过4M,jpg或png格式。</div>
            <div class="btn" @click="upload()" >{{isEdit ? '重新上传' : '点击上传'}}</div>
            <form name="form" id="form">
              <input type="file" id="file">
            </form>
          </div>
        </div> -->
      </div>
      <div class="clear"></div>
      <div class="list" style="margin-top:110px;">
        <img class="red" src="../../images/main/red.png" alt>
        <span class="txt">经营区域：</span>
        <!-- <div class="radio">
          <div v-for="(item,index) in areaList" :key="index">
            <input type="radio" :id="'area_'+item.id" v-model="userInfo.area" :checked="(userInfo.area==item.id)" :value="item.id"  name="area" />
          <label :for="'area_'+item.id">{{item.regionName}}</label>
          </div> -->
        <!-- </div> -->
        <div style="display: inline-block;">
          <el-checkbox
            v-for="(item,index) in areaList"
            :key="item.id"
            v-model="item.selected"
            >
            {{ item.regionName }}
          </el-checkbox>
        </div>

      </div>
      <div class="list">
        <img class="red" src="../../images/main/red.png" alt>
        <span class="txt">服务商加盟期限：</span>
        <input type="text"  ref="input" class="input date" v-model="inpYear"><span class="year" >年</span>
      </div>
      <div class="submit" @click="editParMan()">提交</div>
      </div>
  </div>
</template>
<script>
import Global from '../tool/Global.vue'
import axios from 'axios'
import qs from 'qs'
export default {
  created () {
    this.getArea();
  },
  data () {
    return {
      inpYear: '',
      areaList: [],
      userInfo:{
        partnerName: '',
        partnerAccount: '',
        partnerPhone: '',
        contacts: '',
        joinAge: '',
        businessLicense: '',
        area: '',
        id: this.$store.state.partnerId
      },
      title:'新增服务商',
      areaList: [],
      isEdit:false
    }
  },
  methods: {
    backParMan: function () {
     this.$router.push({
       path: '/main',
       name: 'parMan'
     })
    },
    /**
     * 获取区域
     */
    getArea: function () {
      let that = this;
      axios({
        method:'post',
        async: false,
        url:`${that.$store.state.baseUrl}/backstage/api/tRegion/selectAll`,
      }).then(response => {
        let res = response.data;
        if (res.code == 0) {
        // that.areaList = res.data.data
          if ( res.data ) {
            res.data.forEach ( item => {
              item.selected = false;
              that.areaList.push ( item );
            });
          }
        }
      })
    },

    elUploadSuccess( response, file, item ) {
      // item.roomImg = URL.createObjectURL(file.raw);
      // console.log ( param, file );
      if ( response.code == 0 ) {
        this.userInfo.businessLicense = response.data.src;
      }
      this.$alert ( '上传成功' );
    },
    elUploadBefore( param ) {
      console.log ( param );
    },
    /**
     * 保存服务商
     */
    editParMan: function () {
      let that = this
      let _this = this;
      let url = `${that.$store.state.url}/tService/addService`
      if( this.$route.name == 'editServer' ) {
        url = `${that.$store.state.url}/tService/updateService`
      }
      that.userInfo.id = that.$route.query.id
      axios({
        method:'post',
        url:url,
        data: that.userInfo
      }).then ( res => {
        if (res.data.code == 0) {
          that.$alert ( this.$route.name == 'editServer' ? '修改成功' : '保存成功' ).then (() => {
            that.$route.push({
              path:'/main/server'
            })
          });
        } else {
          that.$alert ( res.data.msg );
        }
      }, response => {
        let res = response.data;
        that.$alert ('系统错误!');
      });

    },
    /**
     * 获取服务商详情
     */
    getParManDetails: function() {
      let that = this;
        axios({
          method:'post',
          url:`${that.$store.state.url}/tPartner/selectPartnerDateils`,
          data:qs.stringify({
            id: this.$store.state.partnerId
          })
          // data: {
          //   id: this.$store.state.partnerId
          // }
        }).then(res => {
            if (res.data.code == 0) {
              for ( let i in that.userInfo ) {
                if ( i == 'area' ) {
                  let arr = res.data.data.area.split ( ',' );
                  for ( let j = 0, len = that.areaList.length; j < len; j ++ ) {
                    for ( let k = 0; k < arr.length; k ++ ) {
                      if ( arr[k] == that.areaList[j].id ) {
                        that.areaList[j].selected = true;
                      }
                    }
                  }
                } else if ( i == 'joinAge' ) {
                  that.inpYear = res.data.data.joinAge;
                } else {
                  that.userInfo[i] = res.data.data[i];
                }
              }
            } else {
              this.$alert ( res.msg );
            }
          });
    },
    upload: function () {
      // let tag = document.getElementById('file')
      this.$refs.input.click()
      // tag.onchange = function () {
      //   let data =  new FormData(tag);
      //   data.append('name','123')
      // }
    }
    

  },
  mounted () {
    if( this.$route.name == 'editServer' ) {
      this.title = '修改服务商'
      this.isEdit = true
      this.getParManDetails()
    }

  },
   watch: {
    inpYear( val ) {
      if(Number(val) > 1000) {
        val = 1000
      }
      this.userInfo.joinAge = Number ( val );
    },
    areaList: {
      deep: true,
      handler ( val ) {
        this.userInfo.area = '';
        for ( let i = 0, len = val.length; i < len; i ++ ) {
          if ( val[i].selected ) {
            this.userInfo.area += val[i].id;
            if ( i < val.length - 1 ) {
              this.userInfo.area += ',';
            }
          }
        }
      }
    }
  }
};
</script>
<style scoped>
.wrap {
  padding: 68px 136px;
  width: 100%;
}
.title {
  height: 33px;
  line-height: 33px;
  font-size: 24px;
  font-family: PingFangSC-Medium;
  font-weight: bold;
}
.title span {
  float: left;
}
.title img {
  width: 28px;
  height: 26px;
  float: left;
  padding-top: 5px;
  margin-right: 10px;
}
.title .right {
  width: 11px;
  height: 18px;
  padding-top: 9px;
  margin-left: 5px;
  margin-right: 6px;
}
.body {
  background: #ffffff;
  box-shadow: 0 5px 20px 0 rgba(0, 0, 0, 0.05);
  border-radius: 6px;
  width: 1490px;
  height: 1237px;
  margin-top: 70px;
  margin-left: 48px;
  padding-left: 233px;
  padding-top: 90px;
  box-sizing: border-box;
}
.list {
  height: 40px;
}
.list {
  height: 40px;
  font-family: PingFangSC-Medium;
  font-size: 18px;
  color: #000000;
  line-height: 40px;
  margin-top: 49px;
}
.firlist {
  margin-top: 0;
}
.red {
  width: 10px;
  height: 10px;
}
.txt {
  width: 154px;
  display: inline-block;
  text-align: left;
  font-family: PingFangSC-Medium;
  font-size: 18px;
  font-weight: bold;
  color: #000000;
}
.input {
  background: #ffffff;
  box-shadow: inset 0 1px 3px 0 rgba(0, 0, 0, 0.5);
  width: 502px;
  height: 40px;
  margin-left: 25px;
  padding-left: 10px;
  font-family: PingFangSC-Medium;
  font-size: 18px;
  color: #888888;
  box-sizing: border-box;
  text-align: center;
}
.tips {
  font-family: PingFangSC-Regular;
  font-size: 14px;
  color: #727272;
  margin-left: 35px;
}
.upload {
  margin-top: 55px;
}
.uptitle {
  height: 25px;
  line-height: 25px;
  font-family: PingFangSC-Medium;
  font-size: 18px;
  color: #000000;
  font-weight: bold;
}
.upimg {
  margin-top: 21px;

}

.upimg img {
  width: 320px;
  height: 205px;
  float: left;
  margin-left: 195px;
}
.info {
  width: 475px;
  padding-top: 38px;
  float: left;
  margin-left: 90px;
}
.uptxt {
  font-family: PingFangSC-Medium;
  font-size: 18px;
  color: #888888;
  height: 30px;
  line-height: 30px;
  margin-top: 15px;
}
.firtxt {
  margin-top: 0;
}
.btn {
  width: 255px;
  height: 55px;
  background: #085dab;
  font-family: PingFangSC-Medium;
  font-size: 18px;
  color: #ffffff;
  margin-top: 47px;
  text-align: center;
  line-height: 55px;
}
.clear {
  clear: both;
}
.radio {
  display: inline-block;
  padding-left: 25px;
}
.radio img {
  width: 25px;
  height: 25px;
  float: left;
  border-radius: 50%;
}
.radio span {
  width: 37px;
  height: 25px;
  line-height: 25px;
  margin-left: 17px;
  float: left;
  margin-right: 59px;
  font-weight: bold;
}
.date {
  width: 172px;
  border-right: 0px;
}
.year {
  background: #085dab;
  border: 1px solid #979797;
  width: 45px;
  height: 38px;
  font-family: PingFangSC-Medium;
  font-size: 18px;
  color: #ffffff;
  line-height: 38px;
  text-align: center;
  display: inline-block;
  font-weight: bold;
}
.submit {
  background: #085dab;
  width: 255px;
  height: 55px;
  font-family: PingFangSC-Medium;
  font-size: 18px;
  color: #ffffff;
  line-height: 55px;
  text-align: center;
  margin-top: 83px;
  margin-left: 374px;
}
.back {
  cursor: pointer;
}
#form {
  position: absolute;
  z-index: -999;
  opacity: 0;
}
input[type="radio"] + label::before {
    content: "\a0"; /*不换行空格*/
    display: inline-block;
    vertical-align: middle;
    /* font-size: 18px; */
    width: 8px;
    height: 8px;
    margin-right: .4em;
    border-radius: 50%;
    border: 1px solid #085dab;
    line-height: 1;
    box-shadow:0px 0px 2px 0 rgba(0,0,0,0.50);
    border: 8px solid #fff;
}
input[type="radio"]:checked + label::before {
    background-color: #fff;
    background-clip: content-box;
    border: 8px solid #085dab;

}
input[type="radio"] {
    position: absolute;
    clip: rect(0, 0, 0, 0);
    margin-left: 58px;
}
.radio > div {
  float: left;
}
.radio label {
  margin-left: 58px;
}
.radio label:first-of-type {
  margin-left: 0px;
}
</style>
